﻿Public Class CheckIn
    Dim no As Int16
    Dim reid As Integer
    Private Sub CheckIn_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtcheckNo.Text = NoGenerater.GenerateNewChecinNo
        txtcheckNo.Enabled = False
        txttotal.Enabled = False
        txtprice.Enabled = False
        txttroom.Enabled = False

        loadctype()
        loadclient()
        loadrtype()
        loadroom()
        loadguest()
        UI2.FormatDataGrid(dgv)
        UI2.FormatDataGrid(dgvavailable)
        loadroomavalable()
    End Sub

    Private Sub loadroomavalable()
        cmd.Parameters("@start") = dtpstart.Value
        cmd.Parameters("@end") = dtpend.Value
        Dim sql = "SELECT r.RoomNo,r.RoomPrice FROM Room as R Inner Join Room as S on R.RoomId=S.RoomId where R.RoomId not in (Select rd.RoomId from Reservation r inner join ReservationDetail rd on rd.ReservationId=r.ReservationId where r.Active =1 and R.DateIn < @end AND r.DateOut > @start ) and S.RoomId not in (SELECT cd.RoomId FROM Checkin c INNER JOIN CheckInDetail cd on cd.CheckInId=c.CheckInId where c.active = 1 and c.DateIn < @end AND c.DateOut > @start) "
        dgvavailable.DataSource = cmd.ExecuteDataTable(sql)
    End Sub

    Private Sub btnnewcus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnewcus.Click
        Dim frm As New ClientAdd
        frm.ShowDialog()
    End Sub

    Private Sub loadclient()
        cbocus.DisplayMember = "ClientName"
        cbocus.ValueMember = "ClientId"
        cbocus.DataSource = cmd.ExecuteDataTable("SELECT ClientName,ClientId FROM Client Where Active=1")
    End Sub

    Private Sub loadctype()
        cboctype.DisplayMember = "ReservationType"
        cboctype.ValueMember = "ReservationTypeId"
        cboctype.DataSource = cmd.ExecuteDataTable("SELECT reservationTypeId,ReservationType FROM ReservationType WHERE Active=1")
    End Sub

    Private Sub loadroom()
        cmd.Parameters("@rtype") = cborType.SelectedValue
        cboRoom.DisplayMember = "RoomNo"
        cboRoom.ValueMember = "RoomId"
        cboroom.DataSource = cmd.ExecuteDataTable("SELECT roomid,roomno FROM room WHERE @rType=0 or roomtypeid=@rtype AND active=1")
    End Sub
    Private Sub loadrtype()
        cborType.DisplayMember = "roomtype"
        cborType.ValueMember = "roomtypeid"
        cborType.DataSource = cmd.ExecuteDataTable("SELECT roomtypeid,roomtype FROM roomtype WHERE active=1 UNION SELECT 0,'ALL'")
    End Sub

    Private Sub loadguest()
        cboguest.DisplayMember = "GuestName"
        cboguest.ValueMember = "GuestId"
        cboguest.DataSource = cmd.ExecuteDataTable("SELECT GuestName,GuestId FROM Guest WHERE Active=1")
    End Sub

    Private Sub cborType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cborType.SelectedIndexChanged
        loadroom()
    End Sub

    Private Sub dtpstart_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpstart.ValueChanged
        loadroomavalable()
    End Sub

    Private Sub dtpend_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpend.ValueChanged
        loadroomavalable()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnewguest.Click
        Dim frm As New GuestAdd
        frm.ShowDialog()
    End Sub


    Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click
        Me.Close()
    End Sub

    Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
        Try
            cmd.Begin()
            'cmd.Parameters("@PurchaseId") = Me.txtPurchaseId.Text
            cmd.Parameters("@ChNo") = Me.txtcheckNo.Text
            cmd.Parameters("@chtype") = Me.cboctype.SelectedValue
            cmd.Parameters("@ClientId") = Me.cbocus.SelectedValue
            cmd.Parameters("@empid") = 1
            cmd.Parameters("@adult") = txtadult.Text
            cmd.Parameters("@child") = txtchild.Text
            cmd.Parameters("@DateIn") = Me.dtpdatein.Value
            cmd.Parameters("@DateOut") = Me.dtpdateout.Value
            cmd.Parameters("@date") = Now.Date
            cmd.Parameters("@Totalroom") = Me.txttroom.Text
            cmd.Parameters("@totalprice") = Me.txttotal.Text


            'If Me.txtPurchaseId.Text = "" Then
            cmd.ExecuteNonQuery("INSERT INTO CheckIn VALUES(@ChNo,@chtype,@ClientId,@empid,@adult,@child,@DateIn,@DateOut,@date,@Totalroom,@totalprice,NULL,1);")
            'Else
            'cmd.ExecuteNonQuery("UPDATE Purchase SET PurchaseNo=@PurchaseNo,PurchaseDate=@PurchaseDate,VendorId=@VendorId,StoreId=@StoreId,Subtotal=@Subtotal,Discount=@Discount,Total=@Total WHERE PurchaseId=@PurchaseId;")
            'End If

            Dim id = cmd.ExecuteScalar("SELECT Max(CheckInId) FROM Checkin WHERE Active=1;")
            For Each row As DataGridViewRow In Me.dgv.Rows
                ' add purchase detail
                'cmd.Parameters("@PurchaseDetailId") = row.Cells("PurchaseDetailId").Value
                cmd.Parameters("@CheckInId") = id
                cmd.Parameters("@RoomId") = row.Cells("RoomId").Value
                cmd.Parameters("@price") = row.Cells("rprice").Value
                cmd.Parameters("@guest") = row.Cells("guestId").Value

                'If row.Cells("PurchaseDetailId").Value Is Nothing Then
                cmd.ExecuteNonQuery("INSERT INTO CheckInDetail VALUES(@CheckInId,@RoomId,@price,@guest,1);")
                'Else
                'cmd.ExecuteNonQuery("UPDATE PurchaseDetail SET ProductId=@ProductId,Qty=@Qty,Price=@Price,Amount=@Amount WHERE PurchaseDetailId=@PurchaseDetailId")
                'End If
                If reid > 0 Then
                    cmd.Parameters("@reid") = reid
                    cmd.ExecuteNonQuery("UPDATE Reservation SET Active=0 WHERE ReservationId=@reid")

                    cmd.Parameters("@reid") = reid
                    cmd.ExecuteNonQuery("UPDATE ReservationDetail SET Active=0 WHERE ReservationId=@reid")
                End If
                
            Next
            MessageBox.Show("CheckIn Sucess")
            cmd.Commit()
            Me.Close()
            'Me.DialogResult = Windows.Forms.DialogResult.OK
        Catch ex As Exception
            cmd.Rollback()
            MsgBox(ex.Message)
        End Try
    End Sub


    Private Sub btnviewreserv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnviewreserv.Click

        'Dim frm As New ViewReservation
        'If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
        '    ReservationBrowser()
        'End If
        ReservationBrowser()
    End Sub
    Private Sub ReservationBrowser()
        'MessageBox.Show("ok")
        Dim frm As New ViewReservation()
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim row = frm.DataGridView1.SelectedRows(0)
            Dim id = row.Cells("ReservationId").Value
            Me.dtpdatein.Value = row.Cells("DateIn").Value
            Me.dtpdateout.Value = row.Cells("DateOut").Value
            Me.cboctype.SelectedValue = row.Cells("ReservationTypeId").Value
            Me.cbocus.SelectedValue = row.Cells("ClientId").Value
            Me.txttroom.Text = row.Cells("TotalRoom").Value
            Me.txttotal.Text = "0"
            'MessageBox.Show(row.Cells("DateIn").Value)
            'Exit Sub
            cborType.Enabled = False
            Dim sql = <sql>
                            SELECT r.RoomId,r.RoomNo
                            FROM ReservationDetail  rd
                            INNER JOIN Room r ON r.RoomId=rd.RoomId
                            WHERE ReservationId=@id
                      </sql>
            cmd.Parameters("@id") = id
            cboroom.DataSource = cmd.ExecuteDataTable(sql)
            dtpend.Visible = False : lblfrom.Visible = False
            dtpstart.Visible = False : lblto.Visible = False
            cmd.Parameters("@id") = id
            dgvavailable.DataSource = cmd.ExecuteDataTable(sql)
            reid = id
        End If
    End Sub

    Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        Dim i = Me.dgv.Rows.Add()
        Dim row = Me.dgv.Rows(i)
        row.Cells("roomid").Value = cboroom.SelectedValue
        row.Cells("roomtype").Value = cborType.Text
        row.Cells("rprice").Value = txtprice.Text
        row.Cells("roomno").Value = cboroom.Text
        row.Cells("guestid").Value = cboguest.SelectedValue
        row.Cells("guestName").Value = cboguest.Text
        'row.Cells("rNo").Value = no
        no += 1
        txttroom.Text = no
        loadtotal()
    End Sub

    Private Sub loadprice()
        cmd.Parameters("@roomid") = cboroom.SelectedValue
        txtprice.Text = cmd.ExecuteScalar("SELECT RoomPrice FROM Room WHERE RoomId=@roomid")
    End Sub

    Private Sub cboroom_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboroom.SelectedIndexChanged
        loadprice()
    End Sub

    Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
        Dim row As DataGridViewRow
        row = dgv.SelectedRows(0)
        Me.dgv.Rows.Remove(row)
        no -= 1
        txttroom.Text = no
        loadtotal()
    End Sub

    Private Sub loadtotal()
        Dim tota As Decimal
        For i = 0 To dgv.Rows.Count - 1
            tota += Parse.ToDecimal(dgv.Rows(i).Cells("rPrice").Value)
        Next
        txttotal.Text = tota
    End Sub

    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

    End Sub

    Private Sub cboctype_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboctype.SelectedIndexChanged

    End Sub
End Class